import nmap
import pprint

from config import *

needed_keys = ['name', 'product', 'reason', 'state', 'version', 'extrainfo', 'script']
needed_proto = ['tcp', 'udp']
def nmap_scan(ip, profile):
    nm = nmap.PortScanner()
    nm.scan(hosts = ip, arguments = profile)
    result = []
    pprint.pprint(nm[ip].keys())
    
    for proto in needed_proto:
        if proto in nm[ip].keys():
            for port, res in nm[ip][proto].items():
                pprint.pprint(port)
                pprint.pprint(res)
                service = dict()
                service.update({'ip' : ip})
                service.update({'port' : str(port)})
                service.update({'proto' : proto})
                for key in needed_keys:
                    if key in res.keys():
                        service.update({key : str(res[key])})
                    else:
                        service .update({key : ''})
                result.append(service)        
    return {'xml' : nm._nmap_last_output, 'services' : result}
    
    
    
if __name__ == '__main__':
    pprint.pprint(nmap_scan('127.0.0.1',NMAP_NORMAL_SCAN))